package org.ogv.usada.views;

import org.ogv.OGV;
import org.ogv.usada.models.GP;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;

public class Option {
	private Sprite _bg;
	private Sprite _slider;
	private Sprite _resumeButton;
	private Sprite _exitButton;
	private Rectangle	_musicSliderArea;
	private Rectangle	_soundSliderArea;
	private boolean		_lastTouchWasResume;
	private boolean		_lastTouchWasExit;
	private Sprite		_debugTex;
	private boolean		_resumeVisible;
	
	public Option(boolean resumeVisible) {
		_resumeVisible	= resumeVisible;
		_bg 			= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_OPTION_BG));
		_slider		 	= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_OPT_SLIDER));
		_resumeButton	= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_OPT_RESUME));
		_exitButton		= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_OPT_EXIT));

		_soundSliderArea = new Rectangle(467, 460, 348, 50);
		_musicSliderArea = new Rectangle(467, 323, 348, 50);
		_resumeButton.setPosition(450, 200);
		_exitButton.setPosition(_resumeVisible ? 760 : 600, 200);
		
		if(GP.$().DEBUG_DRAW) {
			_debugTex = new Sprite(new Texture("data/rect64.png"));
			_debugTex.setOrigin(0, 0);
		}
	}

	public void update(float delta, boolean isTouched, float posX, float posy, Music activeMusic) {
		//Sound and music manipulation
		if(isTouched) {
			if(_musicSliderArea.contains(posX, posy)) {
				float vol = (posX - _musicSliderArea.x) / _musicSliderArea.getWidth();
				GP.$().MUSIC_VOL = vol;
				if(activeMusic != null) activeMusic.setVolume(vol);
			}
			
			else if(_soundSliderArea.contains(posX, posy)) {
				float vol = (posX - _soundSliderArea.x) / _soundSliderArea.getWidth();
				GP.$().SOUND_VOL = vol;
			}
			
			else if(_resumeVisible && _resumeButton.getBoundingRectangle().contains(posX, posy)) {
				GP.$().playClickSound();
				_lastTouchWasResume = true;
			}
			
			else if(_exitButton.getBoundingRectangle().contains(posX, posy)) {
				GP.$().playClickSound();
				_lastTouchWasExit = true;
			}
		}
	}
	
	public void draw(SpriteBatch sb) {
		_bg.draw(sb);
		
		//debug draw
		if(GP.$().DEBUG_DRAW) {
			_debugTex.setScale(_musicSliderArea.getWidth()/64, _musicSliderArea.getHeight()/64);
			_debugTex.setPosition(_musicSliderArea.getX(), _musicSliderArea.getY());
			_debugTex.draw(sb);

			_debugTex.setScale(_soundSliderArea.getWidth()/64, _soundSliderArea.getHeight()/64);
			_debugTex.setPosition(_soundSliderArea.getX(), _soundSliderArea.getY());
			_debugTex.draw(sb);
		}
		
		//Draw sliders
		_slider.setPosition(GP.$().MUSIC_VOL * _musicSliderArea.getWidth() + _musicSliderArea.getX() - _slider.getWidth()/2, _musicSliderArea.getY());
		_slider.draw(sb);
		_slider.setPosition(GP.$().SOUND_VOL * _soundSliderArea.getWidth() + _soundSliderArea.getX() - _slider.getWidth()/2, _soundSliderArea.getY());
		_slider.draw(sb);
		
		//Draw buttons
		if(_resumeVisible) _resumeButton.draw(sb);
		_exitButton.draw(sb);
	}

	public boolean lastTouchWasResume() {
		boolean ret = _lastTouchWasResume;
		_lastTouchWasResume = false;
		return ret;
	}

	public boolean lastTouchWasExit() {
		boolean ret = _lastTouchWasExit;
		_lastTouchWasExit = false;
		return ret;
	}
}
